const { readFileSync, writeFileSync } = require('fs')

; ((async () => {
    const rawData = readFileSync('./data').toString()
    const regions = rawData.split('\n').map(line => {
        const [province, city, area, provinceCode, cityCode, areaCode] = line.split(',')
        return {
            province, city, area, provinceCode, cityCode, areaCode
        }
    })
    const emptyDictCity = []
    const emptyCityProvince = []

    const normalRegionCodes = regions.map(region => {
        const value = `"${region.provinceCode}-${region.cityCode}-${region.areaCode}"`
        if (region.province !== '重庆市' && region.city === region.province && region.province === region.area) {
            emptyCityProvince.push(region)
            return null
            // return `put("${region.province}", ${value});` // 只考虑省份
        } else if (region.province === region.city) {
            emptyDictCity.push(region)
            return null
            // return `put("${region.province}-${region.city}", ${value});` // 只考虑省份 城市
        }
        return `put("${region.province}-${region.city}-${region.area}", ${value});`
    }).filter(region => !!region).join('\n            ')

    const emptyCityProvinceCodes = emptyCityProvince.map(region => {
        const value = `"${region.provinceCode}-${region.cityCode}-${region.areaCode}"`
        return `put("${region.province}", ${value});` // 只考虑省份
    }).join('\n            ')

    const emptyDictCityCodes = emptyDictCity.map(region => {
        const value = `"${region.provinceCode}-${region.cityCode}-${region.areaCode}"`
        return `put("${region.province}-${region.area}", ${value});` // 只考虑省份 城市
    }).join('\n            ')

    // console.log(emptyDictCityCodes)

    const code = `
        /**
         * 这些城市是没有下属地区的，直接省份+城市换code
         */
        static final HashMap emptyDictCityMap = new HashMap<String, String>() {{
            ${emptyDictCityCodes}
        }};

        /**
         * 这些省份是没有下属城市的 直接省份换code
         */
        static final HashMap emptyCityProvinceMap = new HashMap<String, String>() {{
            ${emptyCityProvinceCodes}
        }};

        /**
         * 省份+城市+地区换code
         */
        static final HashMap normalRegionMap = new HashMap<String, String>() {{
            ${normalRegionCodes}
        }};
    `

    writeFileSync('./result', code)
})())